# 类作为依赖：函数就不说了，类其实也是可以执行的，比如创建类实例的方法：__init__

from typing import Annotated, Any

from fastapi import Depends, FastAPI

app = FastAPI()


fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]


class CommonQueryParams:
    def __init__(self, q: str | None = None, skip: int = 0, limit: int = 100):
        self.q = q
        self.skip = skip
        self.limit = limit


# 如果依赖项是一个类，FastAPI 将 “调用” 该类来创建该类本身的实例。
# commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)] 后面的可以简化成 Depends()
@app.get("/items/")
async def read_items(commons: Annotated[CommonQueryParams, Depends()]):
    response = {}
    if commons.q:
        response.update({"q": commons.q})
    items = fake_items_db[commons.skip : commons.skip + commons.limit]
    response.update({"items": items})
    return response